package android.apps.contentprovider;

import android.apps.database.PicDatabaseHelper;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class MyContentProvider extends ContentProvider {
    private static final String DB_NAME = "info.db";
    private static final String DB_TABLE = "info";
    private static final int DB_VERSION = 1;
    private static final String LOG_TAG = ".MyContentProvider";
    private static final UriMatcher uriMatcher;

    public static final String AUTHORITY = "cn.com.eado.mycontentprovider";
//    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/info");
//    public static final Uri CONTENT_URI_ONE = Uri.parse("content://" + AUTHORITY + "/info/#");
    public static final int INFO = 1;//所有的图片信息集合；
    public static final int INFO_PATH = 2;//一个的图片信息集合；

    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITY, "info", INFO);
        uriMatcher.addURI(AUTHORITY, "info/#", INFO_PATH);
    }

    private ContentResolver resolver;
    private PicDatabaseHelper dbHelper;

    public MyContentProvider() {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public String getType(Uri uri) {
                throw new IllegalArgumentException("Unknown URI: " + uri);
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO: Implement this to handle requests to insert a new row.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public boolean onCreate() {
        Context context = getContext();
        resolver = context.getContentResolver();
        dbHelper = new PicDatabaseHelper(context);
        Log.i(LOG_TAG, "Provider Create");
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Log.i(LOG_TAG, "ArticlesProvider.query: " + uri);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        switch (uriMatcher.match(uri)) {
            case INFO://所有的图片信息集合；
                break;
            case INFO_PATH://单个的图片信息集合；
//                long id=ContentUris.parseId(uri);
//                selection="_id=?";
//                selectionArgs=new String[]{String.valueOf(id)};
                String path = uri.getPathSegments().get(0);
                Log.e("LCY","获取的path="+path);
                selection="path=?";
                selectionArgs=new String[]{path};
                break;
            default:
                throw new IllegalArgumentException("Error Uri: " + uri);
        }
        Cursor cursor = db.query("info", projection, selection,selectionArgs, null, null, sortOrder);
        cursor.setNotificationUri(resolver, uri);
        return cursor;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
